﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.ProductionOrders;
using SmartProduction.Domain.yjh;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.BasicCeate;

namespace SmartProduction.Write.API.Application.CommandHandlers.BasicCeateCreateHandler
{
    public class ZhijianBasicinformationcreatecommandHandler : IRequestHandler<ZhijianBasicinformationcreatecommand, APIResult<object>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<Basicinformation> BasicinformationRepository;

        public ZhijianBasicinformationcreatecommandHandler(IMapper mapper, IBaseRepository<Basicinformation> basicinformationRepository)
        {
            this.mapper = mapper;
            BasicinformationRepository = basicinformationRepository;
        }

        public async Task<APIResult<object>> Handle(ZhijianBasicinformationcreatecommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var entity = mapper.Map<Basicinformation>(request);
            var r = BasicinformationRepository.AddAsync(entity).Result;

            if (r > 0)
            {
                result.Code = APIEnums.添加成功;
                result.Msg = APIEnums.添加成功.ToString();
                result.data = r;
            }
            else
            {
                result.Code = APIEnums.添加成功;
                result.Msg = APIEnums.添加成功.ToString();
                result.data = r;
            }

            return await Task.FromResult(result);
        }
    }
}
